import 'dart:convert';

import 'package:wu_you/data/api/api.dart';
import 'package:wu_you/http/http.dart';
import 'package:wu_you/model/home/mall_model.dart';
import 'package:wu_you/model/mall/mall_model.dart';
import 'package:wu_you/model/mall/order_model.dart';

class MallRepository{
  factory MallRepository() => _getInstance();
  static MallRepository _instance;

  MallRepository._internal();

  static MallRepository _getInstance() {
    if (_instance == null) {
      _instance = MallRepository._internal();
    }
    return _instance;
  }

  ///商城分类
  static Future<List<MallCategoryModel>> category({int id}) async {
    List<dynamic> result = await Http().get(Api.categoryList,params: {"nationality_id":id});
    List<MallCategoryModel> data = [];
    if(result != null){
       data = result.map((e) => MallCategoryModel.fromJson(e)).toList();
    }
    return data;
  }

  ///采购分类
  static Future<List<MallCategoryModel>> purchaseCategory({int id,String type}) async {
    List<dynamic> result = await Http().get(Api.categoryList,params: {"nationality_id":id,"type":type});
    List<MallCategoryModel> data = [];
    if(result != null){
      data = result.map((e) => MallCategoryModel.fromJson(e)).toList();
    }
    return data;
  }

  ///获取分组商品
  static Future<List<MallProductModel>> getGroupProduct({String nationality_id, String category_id,int page, int limit,String goods_type}) async {
    Map<String,dynamic> data = {
      "nationality_id":nationality_id,
      "category_id":category_id,
      "page":page,
      "limit":limit,
      "goods_type":goods_type
    };
    dynamic mapResult = await Http().get(Api.goodsList, params: data);
    List<dynamic> list = mapResult['list'];
    List<MallProductModel> mp = [];
    if(list != null && list.length != 0){
      mp = list.map((e) => MallProductModel.fromJson(e)).toList();
    }
    return Future.value(mp);
  }

  ///商品详情
  static Future<GoodsDetail> getNewProductDetail(int productId) async {
    dynamic mapResult = await Http().get(Api.goodsDetail, params: {"goods_id": productId});
    return Future.value(GoodsDetail.fromJson(mapResult));
  }

  ///获取商品SKU详情
  static Future<NewProductSkuModel> getNewProductSkuDetail(String productId, Map<String, dynamic> specs) async {
    dynamic mapResult = await Http().post(Api.goodsSku, data: {"goods_id": productId, "spec_id": jsonEncode(specs)});
    if(mapResult == null){
      return null;
    }
    return Future.value(NewProductSkuModel.fromJson(mapResult));
  }

  ///商品购买
  static Future<dynamic> goodsbuy(String address_id,String sku_id,int order_number,String pay_password,String remark) async {
    Map<String,dynamic> data = {
      "address_id":address_id,
      "sku_id":sku_id,
      "order_number":order_number,
      "pay_password":pay_password,
      "remark":remark
    };
    dynamic mapResult = await Http().post(Api.goodsBuy, data: data);
    return mapResult;
  }

  ///商品购买
  static Future<dynamic> goodsBuyLinglong(String address_id,String sku_id,int order_number,String pay_password,String remark) async {
    Map<String,dynamic> data = {
      "address_id":address_id,
      "sku_id":sku_id,
      "order_number":order_number,
      "pay_password":pay_password,
      "remark":remark
    };
    dynamic mapResult = await Http().post(Api.mallLinglongBuy, data: data);
    return mapResult;
  }

  ///兑换商品购买
  static Future<dynamic> goodsBuyExchange(String address_id,String sku_id,int order_number,String pay_password,String remark) async {
    Map<String,dynamic> data = {
      "address_id":address_id,
      "sku_id":sku_id,
      "order_number":order_number,
      "pay_password":pay_password,
      "remark":remark
    };
    dynamic mapResult = await Http().post(Api.mallExchangeBuy, data: data);
    return mapResult;
  }

  ///商城订单
  static Future<List<OrderListModel>> getOrderList({String state, int page, int size}) async {
    List<dynamic> result = await Http().get(Api.mallOrderList, params: {"order_state": state, "page": page, "limit": size});
    List<OrderListModel> data = [];
    if(result != null){
      data = result.map((e) => OrderListModel.fromJson(e)).toList();
    }else{
      data = [];
    }
    return data;
  }

  ///订单详情
  static Future<OrderListModel> getOrderDetail(String order_id) async {
    dynamic mapResult = await Http().get(Api.mallOrderDetail, params: {"order_id": order_id});
    return Future.value(OrderListModel.fromJson(mapResult));
  }

  ///删除订单
  static Future<dynamic> deleteMallOrderAction(String id) async {
    return Http().get(Api.mallOrderDelete, params: {"order_id": id});
  }

  ///确认收货
  static Future<dynamic> confirmOrderAction(String order_id) async {
    dynamic result = await Http().post(Api.mallOrderConfirm, data: {"order_id": order_id});
    return result;
  }
}